package com.hollysys.hollimsa.biz.istio.virtualservice.controller;

import com.hollysys.hollimsa.biz.istio.virtualservice.domain.VirtualserviceDTO;
import com.hollysys.hollimsa.biz.istio.virtualservice.domain.VirtualserviceDetailDTO;
import com.hollysys.hollimsa.biz.istio.virtualservice.domain.VirtualserviceVO;
import com.hollysys.hollimsa.biz.istio.virtualservice.service.VirtualserviceService;
import com.hollysys.hollimsa.common.result.AjaxObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="istio 虚拟服务-路由管理")
@RestController
@RequestMapping(value = "/serviceMesh")
@Validated
public class VirtualserviceController {

    @Autowired
    private VirtualserviceService virtualserviceService;
    /**
     * 增加虚拟服务
     *
     * @param vsvo 虚拟服务
     * @return 创建状态
     */
    @ApiOperation(value = "创建虚拟服务")
    @PostMapping(value = "/virtualService")
    public AjaxObject<Boolean> createVirtualservice(@RequestBody @Validated VirtualserviceVO vsvo) {
       return   AjaxObject.customOk("", virtualserviceService.createOrReplaceVirtualservice(vsvo));
    }

    /**
     * 编辑虚拟服务
     *
     * @param vsvo 虚拟服务
     * @return 编辑状态
     */
    @ApiOperation(value = "编辑虚拟服务")
    @PutMapping(value = "/virtualService")
    public AjaxObject<Boolean> editVirtualservice(@RequestBody @Validated VirtualserviceVO vsvo) {
        return   AjaxObject.customOk("", virtualserviceService.createOrReplaceVirtualservice(vsvo));
    }

    /**
     * 虚拟服务列表
     *
     * @return 虚拟服务列表
     */
    @ApiOperation(value = "虚拟服务列表")
    @GetMapping(value = "/virtualService/")
    public AjaxObject<List<VirtualserviceDTO>> queryVirtualService() {
        return   AjaxObject.customOk("", virtualserviceService.queryVirtualService());
    }

    /**
     * 根据名称查询虚拟服务
     *
     * @param name 虚拟服务名称
     * @return 创建状态
     */
    @ApiOperation(value = "根据名称查询虚拟服务")
    @ApiImplicitParams({
        @ApiImplicitParam(name="name",value="根据名称查询虚拟服务",dataType="string", paramType = "path",example="name")})
    @GetMapping("/virtualService/{name}")
    public AjaxObject<VirtualserviceDTO> queryVirtualServiceByName(@PathVariable String name) {
        return   AjaxObject.customOk("", virtualserviceService.queryVirtualServiceByName(name));
    }


    /**
     * 删除虚拟服务
     *
     * @param name 虚拟服务-路由名称
     * @return 删除状态
     */
    @ApiOperation(value = "删除虚拟服务")
    @ApiImplicitParam(name="name",value="虚拟服务-规则名称",dataType="string", paramType = "path",example="name")
    @DeleteMapping(value = "/virtualService/{name}")
    public AjaxObject<Boolean> deleteVirtualservice(@PathVariable String name) {
        return   AjaxObject.customOk("", virtualserviceService.deleteVirtualService(name));
    }

    /**
     * 虚拟服务详情
     *
     * @param name 虚拟服务名称
     * @return 创建状态
     */
    @ApiOperation(value = "虚拟服务详情")
    @ApiImplicitParam(name="name",value="虚拟服务详情",dataType="string", paramType = "path",example="name")
    @GetMapping(value = "/virtualService/detail/{name}")
    public AjaxObject<VirtualserviceDetailDTO> queryVirtualDetail(@PathVariable String name) {
        return   AjaxObject.customOk("", virtualserviceService.queryVirtualDetail(name));
    }
}
